home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / p9050_diag.c < prev    next >
C/C++ Source or Header  |  2001-04-11  |  16KB  |  465 lines

  1. ////////////////////////////////////////////////////////////////
  2. // File - P9050_DIAG.C
  3. //
  4. // o A simple diagnostics program that lets you access the
  5. //   PLX 9050 registers and local memory. 
  6. // o This program is meant to be used as an example for using the P9050_LIB.H API,
  7. //   you may use it as a skeleton for your driver, or 'cut & paste' parts
  8. //   of it into your device driver code.
  9. // o For a more advanced monitor program, use the standard PLXMON.EXE
  10. //   from PLX.
  11. // 
  12. ////////////////////////////////////////////////////////////////
  13.  
  14. #include "../lib/p9050_lib.h"
  15. #include "../../../samples/shared/pci_diag_lib.h"
  16. #include <stdio.h>
  17.  
  18. // input of command from user
  19. static char line[256];
  20.  
  21. void PLX_EditReg(P9050_HANDLE hPlx)
  22. {
  23.     struct 
  24.     {
  25.         CHAR *name;
  26.         DWORD dwOffset;
  27.         DWORD dwVal;
  28.     } fields[30];
  29.  
  30.     int cmd;
  31.     int i;
  32.     int field_count;
  33.  
  34.     i = 0;
  35.     fields[i].name = "LAS0RR"; fields[i++].dwOffset = 0x00;
  36.     fields[i].name = "LAS1RR"; fields[i++].dwOffset = 0x04;
  37.     fields[i].name = "LAS2RR"; fields[i++].dwOffset = 0x08;
  38.     fields[i].name = "LAS3RR"; fields[i++].dwOffset = 0x0c;
  39.     fields[i].name = "EROMRR"; fields[i++].dwOffset = 0x10;
  40.     fields[i].name = "LAS0BA"; fields[i++].dwOffset = 0x14;
  41.     fields[i].name = "LAS1BA"; fields[i++].dwOffset = 0x18;
  42.     fields[i].name = "LAS2BA"; fields[i++].dwOffset = 0x1c;
  43.     fields[i].name = "LAS3BA"; fields[i++].dwOffset = 0x20;
  44.     fields[i].name = "EROMBA"; fields[i++].dwOffset = 0x24;
  45.     fields[i].name = "LAS0BRD"; fields[i++].dwOffset = 0x28;
  46.     fields[i].name = "LAS1BRD"; fields[i++].dwOffset = 0x2c;
  47.     fields[i].name = "LAS2BRD"; fields[i++].dwOffset = 0x30;
  48.     fields[i].name = "LAS3BRD"; fields[i++].dwOffset = 0x34;
  49.     fields[i].name = "EROMBRD"; fields[i++].dwOffset = 0x38;
  50.     fields[i].name = "CS0BASE"; fields[i++].dwOffset = 0x3c;
  51.     fields[i].name = "CS1BASE"; fields[i++].dwOffset = 0x40;
  52.     fields[i].name = "CS2BASE"; fields[i++].dwOffset = 0x44;
  53.     fields[i].name = "CS3BASE"; fields[i++].dwOffset = 0x48;
  54.     fields[i].name = "INTCSR"; fields[i++].dwOffset = 0x4c;
  55.     fields[i].name = "CNTRL"; fields[i++].dwOffset = 0x50;
  56.     field_count = i;
  57.     do
  58.     {
  59.         int row;
  60.         int col;
  61.         int row_count = field_count/2 + field_count%2;
  62.  
  63.         printf ("\n");
  64.         printf ("Edit PLX 9050 registers\n");
  65.         printf ("--------------------------------\n");
  66.         for (row = 0; row<row_count; row++)
  67.         {
  68.             for (col = 0; col<=1; col++)
  69.             {
  70.                 if (col==0) i = row;
  71.                 else i = row + row_count;
  72.  
  73.                 if (i<field_count)
  74.                 {
  75.                     char buf[10];
  76.                     fields[i].dwVal = P9050_ReadReg(hPlx, fields[i].dwOffset);
  77.                     sprintf(buf, "%08x",fields[i].dwVal);
  78.                     printf ("%2d. %7s : %s     ",i+1, fields[i].name, buf);
  79.                 }
  80.                 if (col==1) printf ("\n");
  81.             }
  82.         }
  83.  
  84.         printf ("99. Back to main menu\n");
  85.         printf ("Choose register to write to, or 99 to exit: ");
  86.         cmd = 0;
  87.         fgets(line, sizeof(line), stdin);
  88.         sscanf (line, "%d",&cmd);
  89.         if (cmd>=1 && cmd <=21)
  90.         {
  91.             i = cmd-1;
  92.             printf ("Enter value to write to %s register (or 'X' to cancel): ",fields[i].name);
  93.             fgets(line, sizeof(line), stdin);
  94.             if (toupper (line[0])!='X')
  95.             {
  96.                 DWORD dwVal;
  97.                 dwVal = 0;
  98.                 sscanf (line,"%x",&dwVal);
  99.                 P9050_WriteReg(hPlx, fields[i].dwOffset, dwVal);
  100.             }
  101.         }
  102.     } while (cmd!=99);
  103. }
  104.  
  105. char *PLX_GetAddrRangeName(P9050_ADDR addrSpace)
  106. {
  107.     return 
  108.         addrSpace==P9050_ADDR_SPACE0 ? "Addr Space 0 - (BAR2)" :
  109.         addrSpace==P9050_ADDR_SPACE1 ? "Addr Space 1 - (BAR3)" :
  110.         addrSpace==P9050_ADDR_SPACE2 ? "Addr Space 2 - (BAR4)" :
  111.         addrSpace==P9050_ADDR_SPACE3 ? "Addr Space 3 - (BAR5)" :
  112.         addrSpace==P9050_ADDR_EPROM ? "EEPROM Addr Space" : "Invalid";
  113. }
  114.  
  115. void PLX_BoardAccess(P9050_HANDLE hPlx, BOOL fLocalAddr)
  116. {
  117.     int cmd, cmd2, i;
  118.     DWORD addr, data;
  119.     P9050_ADDR ad_sp = P9050_ADDR_SPACE0;
  120.     P9050_MODE ad_mode = P9050_MODE_DWORD;
  121.     char *pcMemoryType = fLocalAddr ? "local address" : "offset";
  122.  
  123.     for (; ad_sp<=P9050_ADDR_EPROM && !P9050_IsAddrSpaceActive(hPlx, ad_sp); ad_sp++)
  124.     if (ad_sp>P9050_ADDR_EPROM)
  125.     {
  126.         printf ("No active memory spaces on board!\n");
  127.         return;
  128.     }
  129.  
  130.     do
  131.     {
  132.         printf ("Access the board's %s ranges\n",pcMemoryType);
  133.         printf ("-------------------------------------------\n");
  134.         printf ("(Access to invalid %s may hang the computer!)\n", pcMemoryType);
  135.         printf ("1. Change active memory space: %s\n",PLX_GetAddrRangeName(ad_sp));
  136.         printf ("2. Toggle active mode: %s\n", 
  137.             ad_mode==P9050_MODE_BYTE ? "BYTE (8 bit)" :
  138.             ad_mode==P9050_MODE_WORD ? "WORD (16 bit)" : "DWORD (32 bit)");
  139.         printf ("3. Read from board\n");
  140.         printf ("4. Write to board\n");
  141.         printf ("99. Back to main menu\n");
  142.         printf ("\n");
  143.         printf ("Enter option: ");
  144.         cmd = 0;
  145.         fgets(line, sizeof(line), stdin);
  146.         sscanf (line, "%d",&cmd);
  147.         switch (cmd)
  148.         {
  149.         case 1:
  150.             printf ("Choose memory space:\n");
  151.             printf ("--------------------\n");
  152.             for (i=P9050_ADDR_SPACE0; i<=P9050_ADDR_EPROM; i++)
  153.             {
  154.                 printf ("%d. %s", i, PLX_GetAddrRangeName(i));
  155.                 if (P9050_IsAddrSpaceActive(hPlx, i)) printf ("\n");
  156.                 else printf (" - space not active\n");
  157.             }
  158.             printf ("Enter option: ");
  159.             cmd2 = 99;
  160.             fgets(line, sizeof(line), stdin);
  161.             sscanf (line, "%d",&cmd2);
  162.             if (cmd2>=P9050_ADDR_SPACE0 && cmd2<=P9050_ADDR_EPROM)
  163.             {
  164.                 int new_ad_sp = cmd2;
  165.                 if (P9050_IsAddrSpaceActive(hPlx, new_ad_sp)) ad_sp = new_ad_sp;
  166.                 else printf ("Chosen space not active!\n");
  167.             }
  168.             break;
  169.         case 2:
  170.             ad_mode = (ad_mode + 1) % 3;
  171.             break;
  172.         case 3:
  173.             printf ("Enter %s to read from: ", pcMemoryType);
  174.             fgets(line, sizeof(line), stdin);
  175.             sscanf (line, "%x", &addr);
  176.             switch (ad_mode)
  177.             {
  178.             case P9050_MODE_BYTE:
  179.                 if (fLocalAddr) data = P9050_ReadByte(hPlx, ad_sp, addr);
  180.                 else data = P9050_ReadSpaceByte(hPlx, ad_sp, addr);
  181.                 break;
  182.             case P9050_MODE_WORD:
  183.                 if (fLocalAddr) data = P9050_ReadWord(hPlx, ad_sp, addr);
  184.                 else data = P9050_ReadSpaceWord(hPlx, ad_sp, addr);
  185.                 break;
  186.             case P9050_MODE_DWORD:
  187.                 if (fLocalAddr) data = P9050_ReadDWord(hPlx, ad_sp, addr);
  188.                 else data = P9050_ReadSpaceDWord(hPlx, ad_sp, addr);
  189.                 break;
  190.             }
  191.             printf ("Value read: %x\n", data);
  192.             break;
  193.         case 4:
  194.             printf ("Enter %s to write to: ", pcMemoryType);
  195.             fgets(line, sizeof(line), stdin);
  196.             sscanf (line, "%x", &addr);
  197.             printf ("Enter data to write %s: ",
  198.                 ad_mode==P9050_MODE_BYTE ? "BYTE (8 bit)" :
  199.                 ad_mode==P9050_MODE_WORD ? "WORD (16 bit)" : "DWORD (32 bit)");
  200.             fgets(line, sizeof(line), stdin);
  201.             sscanf (line, "%x",&data);
  202.             switch (ad_mode)
  203.             {
  204.             case P9050_MODE_BYTE:
  205.                 if (fLocalAddr) P9050_WriteByte(hPlx, ad_sp, addr, (BYTE) data);
  206.                 else P9050_WriteSpaceByte(hPlx, ad_sp, addr, (BYTE) data);
  207.                 break;
  208.             case P9050_MODE_WORD:
  209.                 if (fLocalAddr) P9050_WriteWord(hPlx, ad_sp, addr, (WORD) data);
  210.                 else P9050_WriteSpaceWord(hPlx, ad_sp, addr, (WORD) data);
  211.                 break;
  212.             case P9050_MODE_DWORD:
  213.                 if (fLocalAddr) P9050_WriteDWord(hPlx, ad_sp, addr, data);
  214.                 else P9050_WriteSpaceDWord(hPlx, ad_sp, addr, data);
  215.                 break;
  216.             }
  217.             break;
  218.         }
  219.     } while (cmd!=99);
  220. }
  221.  
  222. void WINAPI PLX_IntHandlerRoutine(P9050_HANDLE hPlx, P9050_INT_RESULT *intResult)
  223. {
  224.     printf ("Got interrupt number %d\n", intResult->dwCounter);
  225. }
  226.  
  227. void PLX_EnableDisableInterrupts(P9050_HANDLE hPlx)
  228. {
  229.     int cmd;
  230.  
  231.     printf ("WARNING!!!\n");
  232.     printf ("----------\n");
  233.     printf ("Your hardware has level sensitive interrupts.\n");
  234.     printf ("You must modify the source code of P9050_IntEnable(), in the file p9050_lib.c,\n");
  235.     printf ("to acknowledge the interrupt before enabling interrupts.\n");
  236.     printf ("Without this modification, your PC will HANG upon interrupt!\n");
  237.  
  238.     do
  239.     {
  240.         printf ("Enable / Disable interrupts\n");
  241.         printf ("---------------------------\n");
  242.         printf ("1. %s interrupts\n", P9050_IntIsEnabled(hPlx) ? "Disable" : "Enable");
  243.         printf ("99. Back to main menu\n");
  244.         printf ("\n");
  245.         printf ("Enter option: ");
  246.         cmd = 0;
  247.         fgets(line, sizeof(line), stdin);
  248.         sscanf (line, "%d",&cmd);
  249.         switch (cmd)
  250.         {
  251.         case 1:
  252.             if (P9050_IntIsEnabled(hPlx))
  253.             {
  254.                 printf ("Disabling interrupt Int\n");
  255.                 P9050_IntDisable(hPlx);
  256.             }
  257.             else
  258.             {
  259.                 printf ("Enabling interrupts\n");
  260.                 if (!P9050_IntEnable(hPlx, PLX_IntHandlerRoutine))
  261.                     printf ("failed enabling interrupts\n");
  262.             }
  263.             break;
  264.         }
  265.     } while (cmd!=99);
  266. }
  267.  
  268. void PLX_EEPROMAccess(P9050_HANDLE hPlx)
  269. {
  270.     int cmd;
  271.     DWORD addr;
  272.     DWORD dwData;
  273.  
  274.     do
  275.     {
  276.         printf ("Access the board's serial EERPOM\n");
  277.         printf ("--------------------------------\n");
  278.         if (!P9050_EEPROMValid(hPlx))
  279.             printf ("Note: PLX EEPROM valid BIT is 0\n");
  280.         printf ("1. Display EEPROM content\n");
  281.         printf ("2. Read dword from serial EEPROM on the board\n");
  282.         printf ("3. Write dword to the serial EEPROM on the board\n");
  283.         printf ("99. Back to main menu\n");
  284.         printf ("\n");
  285.         printf ("Enter option: ");
  286.         cmd = 0;
  287.         fgets(line, sizeof(line), stdin);
  288.         sscanf (line, "%d",&cmd);
  289.         switch (cmd)
  290.         {
  291.         case 1:
  292.             for (addr=0; addr<0xff; addr += 4)
  293.             {
  294.                 if (!(addr % 0x10))
  295.                 printf("\n %02x: ", addr);
  296.                 if (!P9050_EEPROMReadDWord(hPlx, addr, &dwData))
  297.                 {
  298.                     printf("\nError occured reading serial EEPROM - %s\n", P9050_ErrorString);
  299.                     break;
  300.                 }
  301.                 printf("%08x  ", dwData);
  302.             }
  303.             printf ("\n");
  304.             break;
  305.         case 2:
  306.             printf ("Enter addr to read from (0-7f): ");
  307.             fgets(line, sizeof(line), stdin);
  308.             sscanf (line, "%x", &addr);
  309.             if (P9050_EEPROMReadDWord(hPlx, addr, &dwData))
  310.                 printf ("Value read: %08x\n", dwData);
  311.              else
  312.                 printf("Error occured reading serial EEPROM - %s\n", P9050_ErrorString);
  313.             break;
  314.  
  315.         case 3:
  316.             printf ("Enter addr to write to (0-7f): ");
  317.             fgets(line, sizeof(line), stdin);
  318.             sscanf (line, "%x", &addr);
  319.             printf ("Enter data to write: ");
  320.             fgets(line, sizeof(line), stdin);
  321.             sscanf (line, "%x",&dwData);
  322.             if (!P9050_EEPROMWriteDWord(hPlx, addr, dwData))
  323.                 printf("Error occured reading serial EEPROM - %s\n", P9050_ErrorString);
  324.  
  325.             break;
  326.  
  327.         default:
  328.             break;
  329.         }
  330.     } while (cmd!=99);
  331. }
  332.  
  333. P9050_HANDLE PLX_LocateAndOpenBoard(DWORD dwVendorID, DWORD dwDeviceID, BOOL fUseInt)
  334. {
  335.     DWORD cards, my_card;
  336.     P9050_HANDLE hPlx = NULL;
  337.  
  338.     if (dwVendorID==0)
  339.     {
  340.         printf ("Enter VendorID: ");
  341.         fgets(line, sizeof(line), stdin);
  342.         sscanf (line, "%x",&dwVendorID);
  343.         if (dwVendorID==0) return NULL;
  344.  
  345.         printf ("Enter DeviceID: ");
  346.         fgets(line, sizeof(line), stdin);
  347.         sscanf (line, "%x",&dwDeviceID);
  348.     }
  349.     cards = P9050_CountCards (dwVendorID, dwDeviceID);
  350.     if (cards==0) 
  351.     {
  352.         printf("%s", P9050_ErrorString);
  353.         return NULL;
  354.     }
  355.     else if (cards==1) my_card = 1;
  356.     else
  357.     {
  358.         DWORD i;
  359.  
  360.         printf("Found %d matching PCI cards\n", cards);
  361.         printf("Select card (1-%d): ", cards);
  362.         i = 0;
  363.         fgets(line, sizeof(line), stdin);
  364.         sscanf (line, "%d",&i);
  365.         if (i>=1 && i <=cards) my_card = i;
  366.         else 
  367.         {
  368.             printf ("Choice out of range\n");
  369.             return NULL;
  370.         }
  371.     }
  372.     // to correct an errata with BIT7 of BAR0 in some 9050 chips, use P9050_OPEN_FIX_BIT7
  373.     if (P9050_Open (&hPlx, dwVendorID, dwDeviceID, my_card - 1, fUseInt ? P9050_OPEN_USE_INT : 0 /* P9050_OPEN_FIX_BIT7 */ ))
  374.         printf ("PLX 9050 PCI card found!\n");
  375.     else printf ("%s", P9050_ErrorString);
  376.     return hPlx;
  377. }
  378.  
  379. int main(int argc, char *argv[])
  380. {
  381.     int cmd;
  382.     P9050_HANDLE hPlx = NULL;
  383.     HANDLE hWD;
  384.     BOOL fUseInt = FALSE; // by default - do not install interrupts
  385.     BOOL fOpenedWithInt = fUseInt;
  386.     
  387.     printf ("PLX 9050 diagnostic utility.\n");
  388.     printf ("Application accesses hardware using " WD_PROD_NAME ".\n");
  389.  
  390.     // make sure WinDriver is loaded
  391.     if (!PCI_Get_WD_handle(&hWD)) return 0;
  392.     WD_Close (hWD);
  393.  
  394.     hPlx = PLX_LocateAndOpenBoard(0x10b5, 0x9050, fUseInt);
  395.  
  396.     do
  397.     {
  398.         printf ("\n");
  399.         printf ("PLX 9050 main menu\n");
  400.         printf ("-------------------\n");
  401.         printf ("1. Scan PCI bus\n");
  402.         printf ("2. Set opening board %s interrupts\n", fUseInt ? "without" : "with");
  403.         printf ("3. Locate/Choose PLX 9050 board (%s interrupts)\n", fUseInt ? "with" : "without");
  404.         if (hPlx)
  405.         {
  406.             printf ("4. PCI configuration registers\n");
  407.             printf ("5. PLX 9050 local registers\n");
  408.             printf ("6. Access address spaces on the board\n");
  409.             printf ("7. Access local address ranges on the board\n");
  410.             if (fOpenedWithInt)
  411.                 printf ("8. Enable / Disable interrupts\n");
  412.             printf ("9. Access serial EEPROM on the board\n");
  413.         }
  414.         printf ("99. Exit\n");
  415.         printf ("Enter option: ");
  416.         cmd = 0;
  417.         fgets(line, sizeof(line), stdin);
  418.         sscanf (line, "%d",&cmd);
  419.         switch (cmd)
  420.         {
  421.         case 1: // Scan PCI bus
  422.             PCI_Print_all_cards_info();
  423.             break;
  424.         case 2: // Set open board with / without interrupts
  425.             fUseInt = !fUseInt;
  426.             break;
  427.         case 3: // Locate PLX 9050 board
  428.             if (hPlx) P9050_Close(hPlx);
  429.             hPlx = PLX_LocateAndOpenBoard(0, 0, fUseInt);
  430.             if (!hPlx) printf ("PLX card open failed!\n");
  431.             fOpenedWithInt = fUseInt;
  432.             break;
  433.         case 4: // PCI configuration registers
  434.             if (hPlx) 
  435.             {
  436.                 WD_PCI_SLOT pciSlot;
  437.                 P9050_GetPciSlot(hPlx, &pciSlot);
  438.                 PCI_EditConfigReg(pciSlot);
  439.             }
  440.             break;
  441.         case 5: // PLX 9050 local registers
  442.             if (hPlx) PLX_EditReg(hPlx);
  443.             break;
  444.         case 6: // Access address spaces on the board
  445.             if (hPlx) PLX_BoardAccess(hPlx, FALSE);
  446.             break;
  447.         case 7: // Access local address ranges on the board
  448.             if (hPlx) PLX_BoardAccess(hPlx, TRUE);
  449.             break;
  450.         case 8: // Enable / Disable interrupts
  451.             if (hPlx && fOpenedWithInt) PLX_EnableDisableInterrupts(hPlx);
  452.             break;
  453.         case 9: // Access serial EEPROM on the board
  454.             if (hPlx) PLX_EEPROMAccess(hPlx);
  455.             break;
  456.         }
  457.     } while (cmd!=99);
  458.  
  459.     if (hPlx) P9050_Close(hPlx);
  460.  
  461.     return 0;
  462. }
  463.  
  464.                                       
  465.